<template>
  <div class="components-income">
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        class="search-form"
        :model="searchForm"
        @submit.prevent
      >
        <div class="input-row">
          <el-form-item
            prop="nickName"
            :label="$t('distributionMsg.nickName')+':'"
          >
            <el-input
              v-model="searchForm.nickName"
              type="text"
              clearable
              :placeholder="$t('distributionMsg.nickName')"
            />
          </el-form-item>
          <el-form-item
            prop="userMobile"
            :label="$t('distribUserWallet.mobileTelNumber')+':'"
          >
            <el-input
              v-model="searchForm.userMobile"
              type="text"
              clearable
              :placeholder="$t('distribUserWallet.mobileTelNumber')"
            />
          </el-form-item>
          <el-form-item
            prop="orderNumber"
            :label="$t('distribIncome.orderNumber')+':'"
          >
            <el-input
              v-model="searchForm.orderNumber"
              type="text"
              clearable
              :placeholder="$t('distribIncome.orderNumber')"
            />
          </el-form-item>
          <el-form-item
            prop="state"
            :label="$t('distribIncome.commissionStatus')+':'"
          >
            <el-select
              v-model="searchForm.state"
              clearable
              :placeholder="$t('distribIncome.commissionStatus')"
            >
              <el-option
                :label="$t('distributionProdLog.pendingSettlement')"
                value="1"
              />
              <el-option
                :label="$t('distribIncome.withdrawable')"
                value="2"
              />
              <el-option
                :label="$t('seckill.loseEffectiveness')"
                value="-1"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t('crud.searchBtn') }}
            </div>
            <div
              class="default-btn"
              @click="onResetForm"
            >
              {{ $t('crud.emptyBtn') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <div class="main-container">
      <div class="table-con">
        <el-table
          ref="noticeListTableRef"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row-low"
          style="width: 100%"
          @sort-change="onSortChange"
        >
          <el-table-column
            :label="$t('distribUserWallet.distributorNickname')"
            prop="nickName"
          >
            <template #default="scope">
              <span class="table-cell-text">{{ scope.row.distributionUser.nickName }}</span>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('distribUserWallet.mobileTelNumber')"
            prop="userMobile"
          >
            <template #default="scope">
              {{ scope.row.distributionUser.userMobile }}
            </template>
          </el-table-column>
          <el-table-column
            width="180"
            :label="$t('distribIncome.orderNumber')"
            prop="orderNumber"
          />
          <el-table-column
            :label="$t('distribIncome.commission')"
            prop="incomeAmount"
            sortable
          />
          <el-table-column
            width="160"
            :label="$t('sys.creationTime')"
            prop="createTime"
            sortable
          />
          <el-table-column
            :label="$t('distribIncome.rewardRype')"
            prop="incomeType"
          >
            <template #default="scope">
              <span v-if="scope.row.incomeType === 1">{{ $t('distribIncome.levelReward1') }}</span>
              <span v-else-if="scope.row.incomeType === 2">{{ $t('distribIncome.secondaryReward') }}</span>
              <span v-else-if="scope.row.incomeType === 3">{{ $t('distribIncome.invitationReward') }}</span>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('order.orderStatus')"
            prop="status"
          >
            <template #default="scope">
              <span v-if="scope.row.order && scope.row.order.status === 1">{{ $t('order.pendingPayment') }}</span>
              <span v-if="scope.row.order && scope.row.order.status === 2">{{ $t('order.toBeShipped') }}</span>
              <span v-if="scope.row.order && scope.row.order.status === 3">{{ $t('order.pendingReceipt') }}</span>
              <span v-if="scope.row.order && scope.row.order.status === 4">{{ $t('order.toBeEvaluated') }}</span>
              <span v-if="scope.row.order && scope.row.order.status === 5">{{ $t('order.success') }}</span>
              <span v-if="scope.row.order && scope.row.order.status === 6">{{ $t('order.fail') }}</span>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('distribIncome.commissionStatus')"
            prop="state"
            align="center"
          >
            <template #default="scope">
              <span v-if="scope.row.state === 1">{{ $t('distributionProdLog.pendingSettlement') }}</span>
              <span v-else-if="scope.row.state === 2">{{ $t('distribIncome.withdrawable') }}</span>
              <span v-else-if="scope.row.state === -1">{{ $t('seckill.loseEffectiveness') }}</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        :total="page.total"
        layout="total, sizes, prev, pager, next, jumper"
        @update:page-size="onPageSizeChange"
        @update:current-page="onPageChange"
      />
    </div>
  </div>
</template>
<script setup>
let tempSearchForm = null // 保存上次点击查询的请求条件
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
const searchForm = reactive({
  userMobile: '',
  orderNumber: '',
  state: ''
})
const dataList = ref([])
/**
 * 获取数据列表
 * @param pageParam
 * @param newData
 */
const getDataList = (pageParam, newData = false) => {
  if (newData || !tempSearchForm) {
    tempSearchForm = JSON.parse(JSON.stringify(searchForm))
  }
  http({
    url: http.adornUrl('/distribution/distributionUserIncome/page/anduser'),
    method: 'get',
    params: http.adornParams(Object.assign(tempSearchForm, {
      current: pageParam ? pageParam.currentPage : 1,
      size: pageParam ? pageParam.pageSize : 10,
      sortParam,
      sortType
    }))
  }).then(({ data }) => {
    page.total = data.total
    page.pageSize = data.size
    page.currentPage = data.current

    dataList.value = data.records
  })
}
onMounted(() => {
  getDataList()
})

// 默认按创建时间倒序
let sortParam = 2 // 0无 1佣金 2创建时间 3更新时间
let sortType = 2 // 0无 1 正序 2倒序
const onSortChange = (data) => {
  // 排序字段 0无 1佣金 2创建时间 3更新时间
  switch (data.prop) {
    case 'incomeAmount': sortParam = 1
      break
    case 'createTime': sortParam = 2
      break
    case 'updateTime': sortParam = 3
      break
    default: sortParam = 0
      break
  }
  // 排序类型 0无 1 正序 2倒序
  switch (data.order) {
    case 'descending': sortType = 2
      break
    case 'ascending': sortType = 1
      break
    default: sortType = 0
      break
  }
  getDataList(page)
}

/**
 * 点击查询
 * @param newData
 */
const onSearch = (newData = false) => {
  page.currentPage = 1
  getDataList(page, newData)
}
const onPageSizeChange = (val) => {
  page.pageSize = val
  getDataList(page)
}
const onPageChange = (val) => {
  page.currentPage = val
  getDataList(page)
}

/**
 * 重置搜索表单
 * @type {*}
 */
const searchFormRef = ref(null)
const onResetForm = () => {
  searchFormRef.value.resetFields()
}
</script>
